package com.example.administrator.yuejian23.Fragment;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RadioGroup;

import com.example.administrator.yuejian23.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;


public class CenterFragment extends Fragment {
    List<ImageView> list;
    String[] text={"你傻吗","还好","你傻吗","还好"};
    int[] imgSource={R.drawable.top_2,R.drawable.top_1,R.drawable.top_2,R.drawable.top_1};
    private ViewPager viewPager;
    private FrameLayout layout;
    private RadioGroup radioGroup;
    private View view;
    private Timer timer;
    private  int currentPosition;
    private  int oldPosition=0;
    private ActivityFragment activityFragment;
    private ContestFragment contestFragment;
    private MonsterFragment monsterFragment;
    private FragmentManager manager;
    private FragmentTransaction transaction;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view= inflater.inflate(R.layout.fragment_center, container, false);
        init();
        Carousel();//用于处理轮播图的函数
        event();
        return view;
}
    public void event()
    {
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                switch (checkedId)
                {
                    case R.id.activity:
                        replace(activityFragment);
                        break;
                    case  R.id.monster:
                        replace(monsterFragment);
                        break;
                    case R.id.contest:
                        replace(contestFragment);
                        break;

                }
            }
        });
    }
    public void init() {
        viewPager = (ViewPager) view.findViewById(R.id.vp);
        layout = (FrameLayout) view.findViewById(R.id.yuejian_frameLayout);
        radioGroup = (RadioGroup) view.findViewById(R.id.btn_Center);
        activityFragment = new ActivityFragment();
        monsterFragment = new MonsterFragment();
        contestFragment = new ContestFragment();
        manager = this.getChildFragmentManager();
        transaction = manager.beginTransaction();
        transaction.add(R.id.yuejian_1, activityFragment);
        transaction.commit();
    }
    public  void replace(Fragment fragment)
    {
        transaction=manager.beginTransaction();
        transaction.replace(R.id.yuejian_1,fragment);
        transaction.commit();
    }
    public void Carousel()
    {
        //用于存放图片资源
        list=new ArrayList<ImageView>();
        viewPager.setAdapter(pagerAdapter);
        viewPager.addOnPageChangeListener(onPageChangeListener);
    }

    PagerAdapter pagerAdapter=new PagerAdapter() {
        @Override
        public int getCount() {
            return imgSource.length;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            position = position%imgSource.length;
            ImageView imageView = new ImageView(getActivity());
            imageView.setImageResource(imgSource[position]);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            container.addView(imageView);
            list.add(imageView);
            return imageView;
        }
        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(list.get(position));
        }
    };

    ViewPager.OnPageChangeListener onPageChangeListener=new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            int index=position;
        }

        @Override
        public void onPageSelected(int position) {
            oldPosition=position;
            currentPosition=position;
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    };


    @Override
    public void onStart() {
        // TODO Auto-generated method stub
        super.                                                                     onStart();
        ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
        scheduledExecutorService.scheduleWithFixedDelay(
                new ViewPageTask(),
                4,
                4,
                TimeUnit.SECONDS);
    }

    class ViewPageTask implements Runnable {
        @Override
        public void run() {
            currentPosition=(currentPosition+1)%imgSource.length;
            mHandler.sendEmptyMessage(0);
        }
    }

    private Handler mHandler = new Handler(){
        public void handleMessage(android.os.Message msg) {
            viewPager.setCurrentItem(currentPosition);
        };
    };
}
